use iced::{button, Align, Application, Button, Column, Element, Row, Settings, Text};
use strum::IntoEnumIterator;
mod feed_plot;
mod order_plot;
use feed_plot::FeedPlot;
use order_plot::OrderPlot;
use clipboard::ClipboardProvider;

pub fn main() {
    Center::run(Settings::default())
}
#[derive(Debug, Clone, Copy, strum_macros::EnumIter, strum_macros::AsRefStr)]
pub enum CommandType {
    None,
    FeedPlot,
    OrderPlot,
    Sim,
}
impl Default for CommandType {
    fn default() -> CommandType {
        CommandType::None
    }
}

#[derive(Default)]
struct Center {
    cmd_type: CommandType,
    cmd_type_buttons: [button::State; 5],
    feed_plot: FeedPlot,
    order_plot: OrderPlot,
}

#[derive(Debug, Clone)]
pub enum Message {
    None,
    ChangeCommand(CommandType),
    FeedPlotMachine(usize),
    FeedPlotStart(String),
    FeedPlotEnd(String),
    FeedPlotDuration(String),
    FeedPlotFastFeed(bool),
    FeedPlotMear(usize),
    FeedPlotSymbol(String),
    FeedPlotSymbolAdd,
    FeedPlotSymbolsClear,
    ClipboardCopy(String),

    // order_plot
    OrderPlotMachine(usize),
    OrderPlotStart(String),
    OrderPlotEnd(String),
    OrderPlotDuration(String),
    OrderPlotFastFeed(bool),
    OrderPlotMear(usize),
    OrderPlotSymbol(String),
    OrderPlotSymbolAdd,
    OrderPlotSymbolsClear,
}

impl Application for Center {
    type Executor = iced::executor::Null;
    type Message = Message;
    type Flags = ();

    fn new(_flags: ()) -> (Self, iced::Command<Self::Message>) {
        (Self::default(), iced::Command::none())
    }

    fn title(&self) -> String {
        String::from("Command Center")
    }

    fn update(&mut self, message: Self::Message) -> iced::Command<Self::Message> {
        match message {
            Message::None => {}
            Message::ChangeCommand(cmd_type) => {
                self.cmd_type = cmd_type;
            }
            Message::FeedPlotMachine(idx) => self.feed_plot.machine_idx = idx,
            Message::FeedPlotStart(s) => self.feed_plot.start = s,
            Message::FeedPlotEnd(s) => self.feed_plot.end = s,
            Message::FeedPlotDuration(s) => self.feed_plot.duration = s,
            Message::FeedPlotFastFeed(v) => self.feed_plot.use_fastfeed = v,
            Message::FeedPlotMear(idx) => self.feed_plot.mear_idx = idx,
            Message::FeedPlotSymbol(symbol) => self.feed_plot.symbol = symbol,
            Message::FeedPlotSymbolAdd => {
                if !self.feed_plot.symbol.is_empty() {
                    self.feed_plot.symbols.push(self.feed_plot.symbol.clone());
                    self.feed_plot.symbol = "".to_string();
                }
            }
            Message::FeedPlotSymbolsClear => {
                self.feed_plot.symbols.clear();
            }
            Message::OrderPlotMachine(idx) => self.order_plot.machine_idx = idx,
            Message::OrderPlotStart(s) => self.order_plot.start = s,
            Message::OrderPlotEnd(s) => self.order_plot.end = s,
            Message::OrderPlotDuration(s) => self.order_plot.duration = s,
            Message::OrderPlotFastFeed(v) => self.order_plot.use_fastfeed = v,
            Message::OrderPlotMear(idx) => self.order_plot.mear_idx = idx,
            Message::OrderPlotSymbol(symbol) => self.order_plot.symbol = symbol,
            Message::OrderPlotSymbolAdd => {
                if !self.order_plot.symbol.is_empty() {
                    self.order_plot.symbols.push(self.order_plot.symbol.clone());
                    self.order_plot.symbol = "".to_string();
                }
            }
            Message::OrderPlotSymbolsClear => {
                self.order_plot.symbols.clear();
            }
            Message::ClipboardCopy(s) => {
                let mut context = clipboard::ClipboardContext::new().unwrap();
                context.set_contents(s).unwrap();
            }
        }
        iced::Command::none()
    }

    fn view(&mut self) -> Element<Self::Message> {
        let mut col = Column::new().padding(20);
        let mut cmd_selection_row = Row::new()
            .padding(20)
            .spacing(20)
            .align_items(Align::Center);
        for (ct, button) in CommandType::iter().zip(self.cmd_type_buttons.iter_mut()) {
            cmd_selection_row = cmd_selection_row.push(
                Button::new(button, Text::new(ct.as_ref())).on_press(Message::ChangeCommand(ct)),
            );
        }
        col = col.push(cmd_selection_row);

        col = col.push(Text::new(self.cmd_type.as_ref()).size(30));

        match self.cmd_type {
            CommandType::None => {}
            CommandType::FeedPlot => {
                col = col.push(self.feed_plot.view());
            }
            CommandType::OrderPlot => {
                col = col.push(self.order_plot.view());
            }
            _ => {}
        }

        col.into()
    }
}
